package com.atguigu.srb.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.srb.core.enums.TransTypeEnum;
import com.atguigu.srb.core.hfb.FormHelper;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.mapper.TransFlowMapper;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.bo.TransFlowBO;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.TransFlowService;
import com.atguigu.srb.core.service.UserAccountService;
import com.atguigu.srb.core.util.LendNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户账户 服务实现类
 * </p>
 *
 * @author fun
 * @since 2021-10-29
 */
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    TransFlowService transFlowService;
    @Autowired
    TransFlowMapper transFlowMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public String commitCharge(BigDecimal chargeAmt, Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        String bindCode = userInfo.getBindCode();
        // 查询参数，封装hfb表单
        Map<String, Object> mapParam = new HashMap<>();
        mapParam.put("agentId", HfbConst.AGENT_ID);
        mapParam.put("agentBillNo", LendNoUtils.getNo());// 和汇付宝交互的单号
        mapParam.put("bindCode", bindCode);
        mapParam.put("chargeAmt", chargeAmt);
        mapParam.put("feeAmt", new BigDecimal("0"));
        mapParam.put("notifyUrl", HfbConst.RECHARGE_NOTIFY_URL);//检查常量是否正确
        mapParam.put("returnUrl", HfbConst.RECHARGE_RETURN_URL);
        mapParam.put("timestamp", RequestHelper.getTimestamp());
        String sign = RequestHelper.getSign(mapParam);
        mapParam.put("sign", sign);
        String formStr = FormHelper.buildForm(HfbConst.RECHARGE_URL, mapParam);

        return formStr;
    }

    @Override
    public void hfbNotify(Map<String, Object> stringObjectMap) {
        String bindCode = (String) stringObjectMap.get("bindCode");
        BigDecimal amount = new BigDecimal((String) stringObjectMap.get("chargeAmt"));
        String agentBillNo = (String) stringObjectMap.get("agentBillNo");

        // 修改userAccount金额
        baseMapper.updateAccount(bindCode, new BigDecimal("0"), amount);//绑定码，冻结金额，充值金额

        //生成交易流水
        TransFlowBO transFlowBO = new TransFlowBO(agentBillNo,bindCode,amount, TransTypeEnum.RECHARGE,"充值记录");
        transFlowService.saveTransFlow(transFlowBO);

        //调用mq发送充值短信
        Map<String, Object> mqMap = new HashMap<>();
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("bind_code",bindCode);
        UserInfo userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);
        mqMap.put("mobile",userInfo.getMobile());
        mqMap.put("content",TransTypeEnum.RECHARGE.getTransTypeName());

        rabbitTemplate.convertAndSend("exchange.topic.sms","routing.sms.item", JSON.toJSONString(mqMap));
        

    }



    @Override
    public BigDecimal getAccount(Long userId) {
        QueryWrapper<UserAccount> userAccountQueryWrapper = new QueryWrapper<>();
        userAccountQueryWrapper.eq("user_id",userId);
        UserAccount userAccount = baseMapper.selectOne(userAccountQueryWrapper);
        return userAccount.getAmount();
    }
}
